import './../style/managerPage.scss'
import $ from 'jquery'
import './lib/css/toPage.css';
import Swal from 'sweetalert2'



// 总页数
let total;
// 每页显示数量
let pageSize = 10;
// 页码
let pageIndex = 1;

let startTime = 1577861103//2020-01-01

let endTime = Date.parse(new Date())
// 初始化显示数据
// setPageCount(20)
getList(startTime, endTime)
searchForTime()

setDefaultDate()

// // 设置页面显示数量
function setTotal(count) {
    // count:总的条数
    let page = Math.ceil(count / pageSize)

    return page

}
// 获取列表
function getList(pd, nd) {



    $.ajax({
        type: "GET",
        Headers: {
            "Access-Control-Allow-Origin": "*"
        },
        url: "http://lawyeracloud.seo.com.cn/Api/ApiUn/GetUserInfo",
        data: {
            pd: pd,
            nd: nd,
            pageIndex: pageIndex.toString().substr(0,10),
            pageSize: pageSize
        },
        success: function (result) {
            let list = ''
            //console.log('result', result);
            for (const item of result.UserInfo) {
                list += `
                <tr>
                    <td>${item.name}</td>
                    <td>${item.phone}</td>
                    <td>${item.question}</td>
                    <td>${item.CreateTime}</td>
                </tr>
                `
            }
            $('.table tbody').html(list)

            // 计算页数
            total = setTotal(result.count)

            // 重新渲染页数
            getPage()
        },
        error: function (e) {
            //console.group('error',);
            //console.log(e);
            //console.groupEnd();
        }
    });
}

// 分页
function getPage() {

    //总页数
    $(function () {
        //页面初始化调用 生成页码
        makePage(total, 1);
    });
    //生成页码函数
    function makePage(total, page) {
        var pageHtml = '';
        //点点点的html
        var ddd = '<span class="fy-d"></span><span class="fy-d"></span><span class="fy-d"></span>';
        function createPage(index) { //单页码生成
            if (page == index) {
                //当前页(或选中)样式 多了个selected(换背景色字体色的)
                pageHtml += '<a class="fy-y " onclick="pageClick(this)">' + page + '</a>';
            } else {
                pageHtml += '<a  class="fy-y" onclick="pageClick(this)">' + index + '</a>';
            }
        }

        for (var i = 1; i <= total; i++) {
            //正常生成排列
            createPage(i);
        }

        if (page > 1 && total > 1) { // 上一页
            pageHtml = '<a href="#start" class="previous" onclick="first()">首页</a>' + '<a href="#-1" class="previous" onclick="previous()">上一页</a>' + pageHtml;
        } else {
            pageHtml = '<a href="#start" class="previous" onclick="first()">首页</a>' + '<a href="#-1" class="previous" onclick="previous()">上一页</a>' + pageHtml;
        }
        if (page < total && total > 1) { // 下一页
            pageHtml += '<a href="#+1" class="next" onclick="next()">下一页</a>' + '<a href="#end" class="next" onclick="last()">末页</a>';
        } else {
            pageHtml += '<a href="#+1" class="next">下一页</a>' + '<a href="#end" class="next" onclick="last()" >末页</a>';
        }


        // 设置跳转到第几页

        pageHtml += '跳至  <input type="text" class="pageInput" id="pageInput" onkeydown="changeCurrentPage()" />'


        // 设置每页显示条数

        pageHtml +=
            `
    每页显示
    <select class="count" onchange="pageCount(this)">
        <option value="10">10</option>
        <option value="15">15</option>
        <option value="20">20</option>
        <option value="25">25</option>
        <option value="30">30</option>
        <option value="35">35</option>
        <option value="40">40</option>
        <option value="45">45</option>
        <option value="50">50</option>
    </select>条
    `


        // 页码
        pageHtml += `<span class="fy-count">${page}/${total}</span>`
        //赋值生成html
        $('.rb_bottom').html(pageHtml);


        // 渲染页码选中样式
        let current = $('.fy-y')
        for (let i = 0; i < current.length; i++) {
            if ($(current[i]).text() == pageIndex) {
                $(current[i]).addClass('selected')
                // //console.log('duishangle',);
            }
        }

        // 渲染上下页变化

        $("select.count option").each(function () {

            if ($(this).text() == pageSize) {
                //console.log('1')
                $(this).attr('selected', 'selected');
            }
        });


        // 设置上下页按钮颜色
        if (document.documentElement.clientWidth > 700) {

            // if (page == 1) {
            //     document.querySelectorAll(".previous")[0].setAttribute('style', 'color: #d5d5d5')
            //     document.querySelectorAll(".previous")[1].setAttribute('style', 'color: #d5d5d5')
            // }
            // if (page == total) {
            //     document.querySelectorAll(".next")[0].setAttribute('style', 'color: #d5d5d5')
            //     document.querySelectorAll(".next")[1].setAttribute('style', 'color: #d5d5d5')

            // }
        }




    }
    //首页点击事件
    window.first = function first() {

        //获取当前页
        makePage(total, 1);

        pageIndex = 1
        getList(startTime, endTime)
    }

    //末页点击事件
    window.last = function last() {
        //获取当前页

        makePage(total, total);

        pageIndex = total
        getList(startTime, endTime)
    }

    //上一页点击事件
    window.previous = function previous() {

        if (pageIndex != 1) {
            pageIndex = pageIndex - 1
            getList(startTime, endTime)
        }

        //获取当前页
        // var page = $('.rb_bottom>.selected').text();
        // if (page <= 1) {
        //     return;
        // }
        // makePage(total, page - 1);

    }
    //下一页点击事件
    window.next = function next() {
        //获取当前页
        // var page = $('.rb_bottom>.selected').text();
        // if (page >= total) {
        //     return;
        // }
        // makePage(total, page * 1 + 1);
        if (pageIndex != total) {
            pageIndex = pageIndex + 1
            getList(startTime, endTime)

        }
    }
    //直接点击页数事件
    window.pageClick = function pageClick(that) {
        var page = $(that).html();
        makePage(total, page * 1);

        // 请求页数
        pageIndex = page
        getList(startTime, endTime)

    }

    // 获取页码回车
    window.changeCurrentPage = function changeCurrentPage() {
        if (event.keyCode == 13) {
            let val = $('#pageInput').val()
            if (val == '') {
                Swal.fire({
                    position: 'center',
                    icon: 'error',
                    title: '请输入页码',
                    showConfirmButton: false,
                    timer: 1500
                })
            } else if (val > total || val <= 0) {
                Swal.fire({
                    position: 'center',
                    icon: 'error',
                    title: '页码超出范围',
                    showConfirmButton: false,
                    timer: 1500
                })
            } else {
                pageIndex = val
                getList(startTime, endTime)
            }
        }
    }


    // 设置每页显示条数
    window.pageCount = function pageCount(e) {
        pageSize = $(e).val()
        // //console.log($(e).val(2));
        pageIndex = 1
        getList(startTime, endTime)

    }


}

function searchForTime() {
    $('#search').click(() => {
        let startTimeC = $('#startDate').val()
        let endTimeC = $('#endDate').val()

        if (startTimeC == '' || endTimeC == '') {
            Swal.fire({
                position: 'center',
                icon: 'error',
                title: '必须选择开始时间和结束时间',
                showConfirmButton: false,
                timer: 1500
            })
        } else {

            startTimeC = Date.parse(startTimeC)
            endTimeC = Date.parse(endTimeC)

            // 
            startTime = startTimeC
            endTime = endTimeC
            pageIndex = 1
            getList(startTime, endTime)
        }
    })
}

// 重置情况查询
window.reset = function reset() {
    pageSize = 10;
    pageIndex = 1;

    startTime = 852048000

    endTime = Date.parse(new Date())

    setDefaultDate()
    getList(startTime, endTime)

}

// 查询时间
$('#picker').click(() => {
    // //console.log(123)
    // $(this).children('#picker').click()
})

function setDefaultDate() {
    

    let year = new Date().getFullYear()
    let month = new Date().getMonth()+1
    if (month < 9) month = "0" + month
    let day = new Date().getDate()
    //console.log('year+month+day', year + "-" + month + "-" + day);

    // $('#startDate').val(getDay(-1, '-'));
    $('#startDate').val('');

    $('#endDate').val(year + "-" + month + "-" + day)
}

// 获取基于当前日期的日期
// var yesterday = getDay(-20, '-');		//    -1 代表前一天，-2前两天...
function getDay(num, str) {
    var today = new Date();
    var nowTime = today.getTime();
    var ms = 24*3600*1000*num;
    today.setTime(parseInt(nowTime + ms));
    var oYear = today.getFullYear();
    var oMoth = (today.getMonth() + 1).toString();
    if (oMoth.length <= 1) oMoth = '0' + oMoth;
    var oDay = today.getDate().toString();
    if (oDay.length <= 1) oDay = '0' + oDay;
    return oYear + str + oMoth + str + oDay;
}



